package site.isscloud.jt809;

// 报警处理结果类

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

class AlarmResult {
    private String vehicleNo;      // 车牌号
    private byte vehicleColor;     // 车牌颜色
    private long alarmTime;        // 报警时间
    private int alarmType;         // 报警类型
    private int resultType;        // 处理结果类型 0x01:警告 0x02:处罚
    private String resultDesc;     // 处理结果描述
    
    public AlarmResult(String vehicleNo, byte vehicleColor, long alarmTime, 
                      int alarmType, int resultType, String resultDesc) {
        this.vehicleNo = vehicleNo;
        this.vehicleColor = vehicleColor;
        this.alarmTime = alarmTime;
        this.alarmType = alarmType;
        this.resultType = resultType;
        this.resultDesc = resultDesc;
    }
    
    public byte[] toByteArray() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        
        dos.write(JT809Utils.stringToBytes(vehicleNo, 21));
        dos.writeByte(vehicleColor);
        dos.writeLong(alarmTime);
        dos.writeInt(alarmType);
        dos.writeInt(resultType);
        dos.write(JT809Utils.stringToBytes(resultDesc, 256));
        
        return baos.toByteArray();
    }
    
    public static AlarmResult fromByteArray(byte[] data) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);
        
        String vehicleNo = JT809Utils.bytesToString(dis.readNBytes(21));
        byte vehicleColor = dis.readByte();
        long alarmTime = dis.readLong();
        int alarmType = dis.readInt();
        int resultType = dis.readInt();
        String resultDesc = JT809Utils.bytesToString(dis.readNBytes(256));
        
        return new AlarmResult(vehicleNo, vehicleColor, alarmTime, alarmType, resultType, resultDesc);
    }
    
    public String getVehicleNo() {
        return vehicleNo;
    }

    public byte getVehicleColor() {
        return vehicleColor;
    }

    public long getAlarmTime() {
        return alarmTime;
    }

    public int getAlarmType() {
        return alarmType;
    }

    public int getResultType() {
        return resultType;
    }

    public String getResultDesc() {
        return resultDesc;
    }

    @Override
    public String toString() {
        return String.format("AlarmResult[车牌:%s, 类型:%d, 时间:%tF %tT, 结果类型:%d]",
                vehicleNo, alarmType, alarmTime, alarmTime, resultType);
    }
}
